import api from '../../api';
import { message } from 'antd';
import _ from 'lodash';

export default {
  namespace: 'exchangePledgeM',
  state: {
    holdData: [], //持仓数据
  },
  effects: {
    *queryHold({ payload, callback }, { call, put }) {
      const response = yield call(api.exchangePledge.queryHold, payload);
      if (response) {
        yield put({
          type: 'save',
          payload: {
            holdData: _.cloneDeep(response.data),
          },
        });
        callback(response.data || []);
      } else {
        yield put({
          type: 'save',
          payload: {
            holdData: [],
          },
        });
        callback();
        message.error('持仓信息查询失败!');
      }
    },
    //质押券总额
    *getTotalAsset({ payload, callback }, { call, put }) {
      const response = yield call(api.exchangePledge.getTotalAsset, payload);
      if (response) {
        callback(response.data || []);
      } else {
        callback(undefined);
        message.error('获取标准券信息总额失败!');
      }
    },
    //查询交易所标准券可用
    *getAvailableAsset({ payload, callback }, { call, put }) {
      const response = yield call(api.exchangePledge.getAvailableAsset, payload);
      if (response) {
        callback(response.data || []);
      } else {
        callback(undefined);
        message.error('获取标准券信息可用失败!');
      }
    },
    //查询交易标的
    *queryTradingCode({ payload, callback }, { call, put }) {
      const response = yield call(api.exchangePledge.queryTradingCode, payload);
      if (response) {
        if (response.success) {
          callback(response.data || []);
        } else {
          callback(undefined);
          message.error(response.message || '获取证券代码失败!');
        }
      } else {
        callback(undefined);
        message.error('获取证券代码失败!');
      }
    },
    // 提交质押类型 判断单个债券的 可用数量
    *queryAvailableAmount({ payload, callback }, { call, put }) {
      const response = yield call(api.exchangePledge.queryAvailableAmount, payload);
      if (response) {
        if (response.success) {
          callback(response.data);
        } else {
          callback(undefined);
          message.error(response.message || '获取单个债券的可用数量失败!');
        }
      } else {
        callback(undefined);
        message.error('获取单个债券的可用数量失败!');
      }
    },
    //转回质押类型 判断单个债券的质押数量
    *queryPledgeAmount({ payload, callback }, { call, put }) {
      const response = yield call(api.exchangePledge.queryPledgeAmount, payload);
      if (response) {
        if (response.success) {
          callback(response.data);
        } else {
          callback(undefined);
          message.error(response.message || '获取单个债券的质押数量失败!');
        }
      } else {
        callback(undefined);
        message.error('获取单个债券的质押数量失败!');
      }
    },
    //指令下达
    *saveOrder({ payload, callback }, { call, put }) {
      const response = yield call(api.exchangePledge.saveOrder, payload);
      if (response) {
        if (response.success) {
          callback('success');
        } else {
          callback(undefined);
          let msg = response.message ? response.message : response.errorMsg ? response.errorMsg : '指令下达失败!';
          message.error(msg);
        }
      } else {
        callback(undefined);
        message.error('指令下达失败!');
      }
    },
    //簿记下达
    *saveOrderTradeBook({ payload, callback }, { call, put }) {
      const response = yield call(api.exchangePledge.saveOrderTradeBook, payload);
      if (response) {
        if (response.success) {
          callback('success');
        } else {
          callback(undefined);
          let msg = response.message ? response.message : response.errorMsg ? response.errorMsg : '簿记下达失败!';
          message.error(msg);
        }
      } else {
        callback(undefined);
        message.error('簿记下达失败!');
      }
    },
    //存续管理
    *tradeBookSave({ payload, callback }, { call, put }) {
      const response = yield call(api.exchangePledge.tradeBookSave, payload);
      if (response) {
        if (response.success) {
          callback('success');
        } else {
          callback(undefined);
          let msg = response.message ? response.message : response.errorMsg ? response.errorMsg : '指令下达失败!';
          message.error(msg);
        }
      } else {
        callback(undefined);
        message.error('交易录入失败!');
      }
    },
    //查询指令基础信息数据
    *queryBaseInfo({ payload, callback }, { call, put }) {
      const response = yield call(api.exchangePledge.queryBaseInfo, payload);
      if (response && response.code == 0) {
        callback(response.data);
      } else {
        callback();
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
